package com.shujia.controller;

import com.shujia.bean.Student;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class HbaseController {


    private static Connection connection;

    static {
        Configuration configuration = new Configuration();
        //指定zk的连接地址
        configuration.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");

        try {
            //创建连接
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/queryStudentById")
    public Student queryStudentById(String id) {

        Student student1 = new Student();
        try {
            Table student = connection.getTable(TableName.valueOf("student"));

            //查询数据
            Result result = student.get(new Get(id.getBytes()));


            String name = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
            String age = Bytes.toString(result.getValue("info".getBytes(), "age".getBytes()));
            String gender = Bytes.toString(result.getValue("info".getBytes(), "gender".getBytes()));
            String clazz = Bytes.toString(result.getValue("info".getBytes(), "clazz".getBytes()));

            student1.setId(id);
            student1.setName(name);

            student1.setAge(Integer.parseInt(age));

            student1.setGender(gender);
            student1.setClazz(clazz);

        } catch (IOException e) {
            e.printStackTrace();
        }


        return student1;
    }

}
